#!/usr/bin/python
import os, sys, string, commands
import anydbm

class XXEGO:
	keys = []
	cmds = {}

	def __init__(self):
		self.init()

	def start(self, cmd):
		self.run('logon')
		if cmd != '' :
			self.run(cmd)
		prompt = 'Please input command:'
		while True :
			cmd = raw_input(prompt)
			if cmd == 'q' :
				break
			self.run(cmd)

	def run(self, cmd):
		if self.cmds.has_key(cmd) :
			result = commands.getstatusoutput(self.cmds[cmd])
			for i in range(1, len(result)):
				print result[i]
		else :
			self.printHelp()

	def init(self):
		f = open('/home/fkong/bin/xxego', 'r')
		lines = f.readlines()
		for line in lines:
			if len(line) > 3 and line[0:3] == '###':
				line = line[3:len(line)-1]
				idx = string.find(line, '=')
				key = line[0:idx]
				value = line[idx + 1 : len(line)]
				self.keys.append(key)
				self.cmds[key]=value

	def printHelp(self):
		print ''
		print 'Usage:'
		for key in self.keys:
			print '    ' + key + ':    ' + self.cmds[key]

commands.getstatusoutput('egosh user logon -u Admin -x Admin')
t = XXEGO()
if len(sys.argv) == 1:
	t.start('')
elif len(sys.argv) == 2:
	t.start(sys.argv[1])

###=        egosh service list
###logon=   egosh user logon -u Admin -x Admin
###1=       sudo egosh ego start
###2=       sudo egosh ego restart -f
###3=       sudo egosh ego shutdown -f
###4=       sudo egosh ego info
###l=       egosh  service list
###ll=      egosh service list  -l
###sa=      egosh service start all
###ka=      egosh service stop  all
###s1=      egosh service start plc
###k1=      egosh service stop  plc
###v1=      egosh service view  plc
###s2=      egosh service start WEBGUI
###k2=      egosh service stop  WEBGUI
###v2=      egosh service view  WEBGUI
###s3=      egosh service start ICM
###k3=      egosh service stop  ICM
###v3=      egosh service view  ICM

